package com.foreveross.zillasdk;

import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.darin.cl.http.CLHttpUtil;
import com.darin.cl.http.CLHttpUtil.RequestBobyType;
import com.darin.cl.http.CLHttpUtil.RequestMethod;
import com.darin.cl.task.CLTaskManager;
import com.foreveross.URL;
import com.foreveross.chameleonsdk.CApplication;
import com.foreveross.chameleonsdk.ChameleonSDK;
import com.foreveross.util.DeviceInfoUtil;
import com.foreveross.util.Preferences;
import com.foreveross.util.PropertiesUtil;

/**
 * 请在application 中先调用init()方法
 * 
 * @author Jianfeng.Lao
 * 
 */
public class ZillaManager {
	private static final String TAG = "ZillaManager";
	private static ZillaManager mInstance;
	private CLTaskManager mClTaskManager;

	private ZillaManager() {
		initTaskMananger();
	}

	public static ZillaManager getInstance() {
		if (mInstance == null) {
			mInstance = new ZillaManager();
		}

		return mInstance;
	}

	/**
	 * 初始化线程池
	 */
	private void initTaskMananger() {
		if (mClTaskManager == null) {
			mClTaskManager = new CLTaskManager(50, 5, 5);
		}
	}

	/**
	 * 消除所有线程
	 */
	public void releaseTaskMananger() {
		if (mClTaskManager != null) {
			mClTaskManager.destory();
		}
	}


	public ZillaRequestTask appAuth(Context context, ZillaListener delegate) {
		ZillaRequestTask task = new ZillaRequestTask(context, delegate) {
			@Override
			public String doInBackground(Object... params) throws Exception {
				String url = URL.AUTH;
				mHttpClient = new CLHttpUtil(url);
				PropertiesUtil properties = ChameleonSDK.getCApplication().getCubePropertie();
				String appKey = properties.getString("appKey", "");
				String secret = properties.getString("secret", "");
				mHttpClient.addParam("appKey", appKey);
				mHttpClient.addParam("secret", secret);
				String result = null;
				HttpResponse httpResponse = mHttpClient.obtainResponse(RequestMethod.POST,
						RequestBobyType.FORM_URLENCODED, null);
				if (httpResponse != null) {
					if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
						result = CLHttpUtil.getContentByResponse(httpResponse);
					} else {
						result = (httpResponse.getStatusLine().getStatusCode() + "").trim();
					}
				} else {
					throw new NullPointerException("no http response");
				}
				return result;
			}

		};
		task.setShowErrorDialog(false);
		task.setShowProgressDialog(true);
		task.setDialogContent("正在验证...");
		mClTaskManager.execute(task);
		return task;
	}

	public ZillaRequestTask syncModule(Context context, ZillaListener delegate, boolean dialogNeed,
			final String dialogContent) {

		ZillaRequestTask task = new ZillaRequestTask(context, delegate) {

			@Override
			public String doInBackground(Object... params) throws Exception {
				if (Preferences.getToken().equals("")) {
					throw new NullPointerException("token == null");
				}
				String token = Preferences.getToken();
				long time = new Date().getTime();
				String url = URL.BASE_WS + "mam/api/mam/clients/apps/modules/" + token
						+ "?timeStamp=" + time;
				mHttpClient = new CLHttpUtil(url);
				return mHttpClient.getResponseString(RequestMethod.GET, null);
			}

		};
		task.setShowErrorDialog(dialogNeed);
		task.setShowProgressDialog(dialogNeed);
		task.setDialogContent(dialogContent);
		mClTaskManager.execute(task);
		return task;
	}

	public ZillaRequestTask syncPrivilege(Context context, ZillaListener delegate,
			final String username, boolean dialogNeed, final String dialogContent) {

		ZillaRequestTask task = new ZillaRequestTask(context, delegate) {

			@Override
			public String doInBackground(Object... params) throws Exception {
				String url = URL.BASE_WS + "mam/api/mam/clients/apps/" + URL.APP_PACKAGENAME + "/"
						+ username + "/auth?" + "appKey=" + URL.APPKEY;
				mHttpClient = new CLHttpUtil(url);
				return mHttpClient.getResponseString(RequestMethod.GET, null);
			}

		};
		task.setShowErrorDialog(dialogNeed);
		task.setShowProgressDialog(dialogNeed);
		task.setDialogContent(dialogContent);
		mClTaskManager.execute(task);
		return task;
	}

	public ZillaRequestTask snapshot(Context context, ZillaListener delegate, final String appKey,
			final String identifier, final String version) {
		ZillaRequestTask task = new ZillaRequestTask(context, delegate) {

			@Override
			public String doInBackground(Object... params) throws Exception {
				String url = URL.SNAPSHOT + identifier + "/" + version + "/snapshot?appKey="
						+ appKey;
				mHttpClient = new CLHttpUtil(url);
				return mHttpClient.getResponseString(RequestMethod.GET, null);
			}

		};
		task.setShowErrorDialog(false);
		task.setShowProgressDialog(false);
		task.setDialogContent(null);
		mClTaskManager.execute(task);
		return task;
	}

	/**
	 * 签到
	 * 
	 * @param context
	 * @param delegate
	 * @param checkInVoJson
	 * @return
	 */
	public ZillaRequestTask pushCheckIn(Context context, final ZillaListener delegate,
			final String checkInVoJson) {
		ZillaRequestTask task = new ZillaRequestTask(context, delegate) {
			@Override
			public String doInBackground(Object... params) throws Exception {
				String url = URL.CHECKIN_URL;
				mHttpClient = new CLHttpUtil(url);
				mHttpClient.addHeader("Accept", "application/json");
				mHttpClient.setJsonParam(checkInVoJson);
				HttpResponse response = mHttpClient.obtainResponse(RequestMethod.PUT,
						RequestBobyType.APP_JSON, null);
				String result = "false";
				if (response != null
						&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					Log.v(TAG, "签到成功!");
					result = "true";
				}
				return result;
			}

		};
		task.setShowErrorDialog(false);
		task.setShowProgressDialog(false);
		task.setDialogContent(null);
		mClTaskManager.execute(task);
		return task;

	}

	/**
	 * 拉取推送信息
	 * 
	 * @param context
	 * @param delegate
	 * @param deviceId
	 * @param appId
	 * @return
	 */
	public ZillaRequestTask pushGetMessage(Context context, final ZillaListener delegate,
			final String deviceId, final String appId) {
		ZillaRequestTask task = new ZillaRequestTask(context, delegate) {
			@Override
			public String doInBackground(Object... params) throws Exception {
				String url = URL.GETPUSHMESSAGE + deviceId + "/" + appId;
				mHttpClient = new CLHttpUtil(url);
				return mHttpClient.getResponseString(RequestMethod.GET, null);
			}

		};
		task.setShowErrorDialog(false);
		task.setShowProgressDialog(false);
		task.setDialogContent(null);
		mClTaskManager.execute(task);
		return task;
	}

	/**
	 * 回执消息
	 * 
	 * @param context
	 * @param delegate
	 * @param msgId
	 * @return
	 */
	public ZillaRequestTask pushReceipt(final Context context, final ZillaListener delegate,
			final String msgId) {
		ZillaRequestTask task = new ZillaRequestTask(context, delegate) {
			@Override
			public String doInBackground(Object... params) throws Exception {
				String url = URL.FEEDBACK_URL;
				String appKey = URL.APPKEY;
				mHttpClient = new CLHttpUtil(url);
				mHttpClient.addParam("deviceId", DeviceInfoUtil.getDeviceId(mContext));
				mHttpClient.addParam("msgId", msgId);
				mHttpClient.addParam("appId", appKey);
				return mHttpClient.getResponseString(RequestMethod.POST,
						RequestBobyType.FORM_URLENCODED);
			}
		};
		task.setShowErrorDialog(false);
		task.setShowProgressDialog(false);
		task.setDialogContent(null);
		mClTaskManager.execute(task);
		return task;
	}

}
